package hfuu.com.cn.teasystemapp;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Looper;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import hfuu.com.cn.teasystemapp.bean.land;
import hfuu.com.cn.teasystemapp.bean.pick;
import hfuu.com.cn.teasystemapp.util.HttpUtils;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class pickaddactivity extends AppCompatActivity {

    List<land> landList;
    pick pick;
    String res="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pick=new pick();
        setContentView(R.layout.activity_pickaddactivity);
        Intent old=getIntent();
        landList = new ArrayList<land>();
        landList = (List<land>) old.getSerializableExtra("landlist");
       // Log.i("land",landList.toString());
        ImageView back = (ImageView) findViewById(R.id.pickback);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Spinner sptype= (Spinner) findViewById(R.id.type);
        Spinner spland=(Spinner)findViewById(R.id.landname);
        final EditText teanametext= (EditText) findViewById(R.id.teaname);
        final EditText totaltex= (EditText) findViewById(R.id.total);

        sptype.setDropDownVerticalOffset(30);
        spland.setDropDownVerticalOffset(30);
        List data=new ArrayList();

        for (land land:
             landList) {
            data.add(land.getLandName());
        }
        ArrayAdapter ad=new ArrayAdapter(this,R.layout.landitem,R.id.land,data);
        spland.setAdapter(ad);
        spland.setVisibility(View.VISIBLE);
        Button okb= (Button) findViewById(R.id.pickok);
        Button cab= (Button) findViewById(R.id.pickcancel);
        okb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String teaname=teanametext.getText().toString().trim();
                String total=totaltex.getText().toString().trim();
                if((teaname.length()==0)&&(total.length()==0)){
                    Toast.makeText(pickaddactivity.this,"请输入内容",Toast.LENGTH_SHORT).show();
                    return;
                }
                Pattern pattern=Pattern.compile("^((0)|([1-9][0-9]*))\\.[0-9]+$");
                Pattern p = Pattern.compile("^[-\\+]?[\\d]*$");
                if(!(pattern.matcher(total).matches()||p.matcher(total).matches())){
                    Toast.makeText(pickaddactivity.this,"重量输入数字",Toast.LENGTH_SHORT).show();
                    return;
                }
                pick.setPickTeaname(teaname);
                pick.setPickQuality(Float.parseFloat(total));
                Gson gson=new Gson();
                String pickjson= gson.toJson(pick);
                HttpUtils.doPost("pick/addprocad", pickjson, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Looper.prepare();
                        Toast.makeText(pickaddactivity.this, "网络连接失败", Toast.LENGTH_LONG).show();
                        Looper.loop();
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        res=response.body().string();
                        if(res.equals("success")){
                            Looper.prepare();
                            Toast.makeText(pickaddactivity.this, "插入成功", Toast.LENGTH_LONG).show();
                            Looper.loop();

                            pickaddactivity.this.finish();
                            return;
                        }


                    }
                });
                finish();

            }
        });
        cab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pickaddactivity.this.finish();
            }
        });

        /**
         * 监听下拉列表
         */
        sptype.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String m = (String) parent.getSelectedItem();
                if(m.equals("折采")){
                    pick.setPickOperate(0);
                }else if(m.equals("提手采")){
                    pick.setPickOperate(1);

                }else if(m.equals("双手采")){
                    pick.setPickOperate(2);
                }else if(m.equals("其他")){
                    pick.setPickOperate(3);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        spland.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String m = (String) parent.getSelectedItem();
                for (land land:
                     landList) {
                    if(m.equals(land.getLandName()))
                        pick.setPickLandid(land.getLandId());

                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }
}
